package com.atguigu.logger.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.contants.GmallConstants;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Date:2021/3/16
 * Author:csw
 * Description:
 */
@RestController
@Slf4j
public class LogController {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;


    @RequestMapping(value = "/log")
    public String handle(String logString){
//        Logger logger = LoggerFactory.getLogger(LogController.class);
//        logger.info(logString);
        JSONObject jsonObject = JSON.parseObject(logString);
        jsonObject.put("ts",System.currentTimeMillis());
        String finalString = jsonObject.toString();
        if("startup".equals(jsonObject.getString("type"))){
            kafkaTemplate.send(GmallConstants.KAFKA_TOPIC_STARTUP,finalString);
        }else{
            kafkaTemplate.send(GmallConstants.KAFKA_TOPIC_EVENT,finalString);

        }

        log.info(finalString);
        return  "success";
    }
}
